/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.support.values;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class defines values of the <i>'a lazy_t</i> type.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
public final class Lazy implements ToValue {

    /** OCaml value to be treated as lazy. */
    private final Value value;

    /**
     * Constructs a lazy value.
     * @param v lazy value - should not be <tt>null</tt>
     */
    public Lazy(final Value v) {
        assert v != null : "null v";
        this.value = v;
    } // end constructor(Value)

    /**
     * Tests whether the value is suspended.
     * @return <tt>true</tt> if the value is suspended,
     *         <tt>false</tt> otherwise
     */
    public boolean isSuspended() {
        return this.value.isBlock()
            && (this.value.asBlock().getTag() == Block.LAZY_TAG);
    } // end method 'isSuspended()'

    /**
     * Returns the computed value if not suspended, the computing closure otherwise.
     * @return the computed value if not suspended, the computing closure otherwise
     */
    public Value get() {
        if (this.value.isLong() || this.value.isCodeOffset()) {
            return this.value;
        } else {
            return this.value.asBlock().get(0);
        } // end if/else
    } // end method 'get()'

    /**
     * {@inheritDoc}
     */
    @Override
    public Value toValue() {
        return this.value;
    } // end method 'toValue()'

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return this.value.hashCode();
    } // end method 'hashCode()'

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(final Object obj) {
        if (obj instanceof Reference) {
            final Lazy that = (Lazy) obj;
            return this.get().equals(that.get());
        } else {
            return false;
        } // end if/else
    } // end method 'equals(Object)'

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        if (this.value.isLong() || this.value.isCodeOffset()) {
            return this.value.toString();
        } else {
            if (this.value.asBlock().getTag() == Block.LAZY_TAG) {
                return "lazy " + this.value.asBlock().get(0).toString();
            } else {
                return this.value.asBlock().get(0).toString();
            } // end if/else
        } // end if/else
    } // end method 'toString()'

} // end class 'Lazy'
